<?php

namespace app\index\controller;

use app\BaseController;
use app\common\model\UserModel;
use think\captcha\facade\Captcha;

/** 
 *  前端登录控制器
 * 
 * 时间：2020年3月11日13:08:14
 * 作者：夏立军
 *
 */
class Login extends BaseController
{
    // 登录页面
    public function index()
    {
        return view('index', [
            'system_name' => config('ams_config.system_name'),
            'registered' => config('ams_config.index_registered'),
        ]);
    }

    // 注册
    public function register()
    {

        if (request()->post()) {
            if (!config('ams_config.index_registered')) {
                return json(['code' => 99, 'msg' => '当前禁止用户注册']);
            }

            $account = input('post.account/s', null);
            $password = input('post.password/s', null);
            $password2 = input('post.password2/s', null);
            $name = input('post.name/s', null);
            $captcha = input('post.captcha/s', null);

            if (is_null($account) || is_null($password) || is_null($password2) || is_null($name) || is_null($captcha)) {
                return json(['code' => 1, 'msg' => '填写数据不完整']);
            }

            if ($password != $password2) {
                return json(['code' => 2, 'msg' => '两次密码不一致']);
            }

            if (!Captcha::check($captcha)) {
                return json(['code' => 3, 'msg' => '验证码错误']);
            }

            $user = new UserModel();
            $user_info = $user->get_user_info_by_account($account);

            if ($user_info) {
                return json(['code' => 4, 'msg' => '用户已存在']);
            }
            $password = md5($password . config('ams_config.user_password_suffix'));
            $res = $user->register_user($account, $password, $name);

            if (!$res) {
                return json(['code' => 5, 'msg' => '注册失败']);
            }

            return json(['code' => 0, 'msg' => '注册完成']);
        }
        return view('register', [
            'system_name' => config('ams_config.system_name'),
        ]);
    }

    // 登录 及验证
    public function login()
    {
        if (request()->post()) {
            $account = input('post.account/s', null);
            $password = input('post.password/s', null);
            $captcha = input('post.captcha/s', null);
            if (is_null($account) || is_null($password)) {
                return json(['code' => 1, 'msg' => '输入信息不完整']);
            }
            if (!Captcha::check($captcha)) {
                return json(['code' => 2, 'msg' => '验证码错误']);
            }

            $user = new UserModel();
            $user_info = $user->get_user_info_by_account($account);
            if (!$user_info) {
                return json(['code' => 3, 'msg' => '用户不存在']);
            }

            if (md5($password . config('ams_config.user_password_suffix')) != $user_info['password']) {
                return json(['code' => 4, 'msg' => '密码错误']);
            }

            session('index_login', $user_info);
            return json(['code' => 0, 'msg' => '登录成功']);
        }
        return json(['code' => 99, 'msg' => "提交方式错误"]);
    }

    // 退出
    public function logOut()
    {
        session('index_login', null);
        return redirect_test(url('index/index'));
    }

    // 创建一个验证码
    public function verify()
    {
        return Captcha::create();
    }
}
